/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package quanlisinhvien.pl;

import java.awt.event.ItemEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JDialog;
import javax.swing.JFrame;
import quanlisinhvien.bll.*;
import quanlisinhvien.dto.*;


/**
 *
 * @author Marshmallow
 */
public class F_TimKiem extends JDialog {

    /**
     * Creates new form F_TimKiem
     */
    public F_TimKiem(JFrame parent) {
        super(parent,"", true);
        initComponents();      
        cboKhoa.removeAllItems();
        HienThiDanhSachKhoa();
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        cboKhoa = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        cboLop = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        txtTuKhoa = new javax.swing.JTextField();
        btnTimKiem = new javax.swing.JButton();
        cboTimKiem = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Tìm kiếm");
        setBounds(new java.awt.Rectangle(250, 100, 0, 0));

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel1.setText("Khoa:");

        cboKhoa.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        cboKhoa.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cboKhoa.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cboKhoaItemStateChanged(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel2.setText("Lớp:");

        cboLop.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        cboLop.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setText("Tìm theo:");

        txtTuKhoa.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        btnTimKiem.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        btnTimKiem.setText("Tìm kiếm");
        btnTimKiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTimKiemActionPerformed(evt);
            }
        });

        cboTimKiem.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        cboTimKiem.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Mã sinh viên", "Họ tên", "Địa chỉ" }));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(btnTimKiem, javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(66, 66, 66)
                                .addComponent(txtTuKhoa))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel3))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(cboTimKiem, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(cboLop, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(cboKhoa, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addComponent(jLabel1))
                .addContainerGap(35, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cboKhoa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cboLop, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(cboTimKiem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(txtTuKhoa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
                .addComponent(btnTimKiem)
                .addGap(17, 17, 17))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnTimKiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTimKiemActionPerformed
        SinhVienDTO sinhVien = new SinhVienDTO();
        LopDTO lopChon = new LopDTO();
        KhoaDTO khoaChon = new KhoaDTO();
        if (!cboKhoa.getSelectedItem().equals("Tất cả")){               
                 khoaChon.setTenKhoa((String)cboKhoa.getSelectedItem());
                 lopChon.setKhoa(khoaChon);
        }
        if (!cboLop.getSelectedItem().equals("Tất cả")){           
            lopChon.setTenLop((String) cboLop.getSelectedItem());
           
        }
        sinhVien.setLop(lopChon); 
        switch(cboTimKiem.getSelectedItem().toString()){
            case "Mã sinh viên":
                sinhVien.setMaSinhVien(txtTuKhoa.getText());
                break;
            case "Họ tên":
                sinhVien.setHoTen(txtTuKhoa.getText());
                break;
            case "Địa chỉ":
                sinhVien.setDiaChi(txtTuKhoa.getText());
                break;
        }
        SinhVienBLL qlSinhVien = new SinhVienBLL();
        F_QuanLiSinhVien.danhSachSinhVien = qlSinhVien.TimKiemSinhVien(sinhVien);
        this.dispose();
    }//GEN-LAST:event_btnTimKiemActionPerformed

    private void cboKhoaItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboKhoaItemStateChanged
         Object item = evt.getItem();
        if(evt.getStateChange()  == ItemEvent.SELECTED){
            HienThiDanhSachLop(item.toString());
        }
    }//GEN-LAST:event_cboKhoaItemStateChanged

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(F_TimKiem.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(F_TimKiem.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(F_TimKiem.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(F_TimKiem.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new F_TimKiem(new JFrame()).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnTimKiem;
    private javax.swing.JComboBox cboKhoa;
    private javax.swing.JComboBox cboLop;
    private javax.swing.JComboBox cboTimKiem;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField txtTuKhoa;
    // End of variables declaration//GEN-END:variables
  
    private void HienThiDanhSachKhoa() {   
        cboKhoa.addItem("Tất cả");
        KhoaBLL qlKhoa = new KhoaBLL();
        List<KhoaDTO> danhSachKhoa = qlKhoa.LayDanhSachKhoa();
        for(KhoaDTO khoa : danhSachKhoa){
            cboKhoa.addItem(khoa.getTenKhoa());           
        }
    }
    //Thay đổi list lớp khi chọn qlKhoa
    private void HienThiDanhSachLop(String tenKhoa){        
        cboLop.removeAllItems();
        cboLop.addItem("Tất cả");
        LopBLL qlLop = new LopBLL();
        List<LopDTO> danhSachLop = new ArrayList<LopDTO>();
        if (!tenKhoa.equals("Tất cả") )
            danhSachLop = qlLop.LayDanhSachLopTheoKhoa(tenKhoa);
        else
            danhSachLop = qlLop.LayDanhSachLop();
        for(LopDTO lop : danhSachLop){
            cboLop.addItem(lop.getTenLop());
        }
    }
}
